package com.xxxx.controller;


import com.xxxx.pojo.Position;
import com.xxxx.pojo.R;
import com.xxxx.service.PositionService;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.List;

/**
 * <p>
 * 前端控制器
 * </p>
 *
 * @author chao
 * @since 2022-09-26
 */
@RestController
@RequestMapping("/system/basic/pos")
public class PositionController {

    @Autowired
    private PositionService positionService;

    @ApiOperation(value = "获取所有职位信息")
    @GetMapping("/")
    public R getAllPositions(){
        List<Position> list = positionService.list();
        return R.success(list);
    }

    @ApiOperation(value = "添加职位信息")
    @PostMapping("/")
    public R savePosition(@RequestBody Position position) {
        position.setCreateDate(LocalDateTime.now());
        boolean flag = positionService.save(position);
        return flag ? R.success("添加成功") : R.error("添加失败！");
    }

    @ApiOperation(value = "更新职位信息")
    @PutMapping("/")
    public R updatePosition(@RequestBody Position position) {
        position.setCreateDate(LocalDateTime.now());
        boolean flag = positionService.updateById(position);
        return flag ? R.success("更新成功") : R.error("更新失败！");
    }

    @ApiOperation(value = "单删除职位信息")
    @DeleteMapping("/{id}")
    public R updatePosition(@PathVariable Integer id) {

        boolean flag = positionService.removeById(id);
        return flag ? R.success("删除成功") : R.error("删除失败！");
    }

    @ApiOperation(value = "批量删除职位信息")
    @DeleteMapping("/")
    public R updatePositionByIds(Integer[] ids) {

        boolean flag = positionService.removeByIds(Arrays.asList(ids));
        return flag ? R.success("删除成功") : R.error("删除失败！");
    }

}
